package com.ruoyi.cderp.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.cderp.mapper.TblProductStockMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.TblSaleReturnDetail;
import com.ruoyi.cderp.mapper.TblSaleReturnMapper;
import com.ruoyi.cderp.domain.TblSaleReturn;
import com.ruoyi.cderp.service.ITblSaleReturnService;

/**
 * 销售退单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-31
 */
@Service
public class TblSaleReturnServiceImpl implements ITblSaleReturnService
{
    // 存储当前登录用户对象
    LoginUser loginUser = null;
    @Autowired
    private TblSaleReturnMapper tblSaleReturnMapper;
    @Autowired
    private TblProductStockMapper tblProductStockMapper;

    /**
     * 查询销售退单
     *
     * @param id 销售退单主键
     * @return 销售退单
     */
    @Override
    public TblSaleReturn selectTblSaleReturnById(String id)
    {
        return tblSaleReturnMapper.selectTblSaleReturnById(id);
    }

    /**
     * 查询销售退单列表
     *
     * @param tblSaleReturn 销售退单
     * @return 销售退单
     */
    @Override
    public List<TblSaleReturn> selectTblSaleReturnList(TblSaleReturn tblSaleReturn)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();

        return tblSaleReturnMapper.selectTblSaleReturnList(tblSaleReturn);
    }

    /**
     * 新增销售退单
     *
     * @param tblSaleReturn 销售退单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblSaleReturn(TblSaleReturn tblSaleReturn)
    {
        tblSaleReturn.setCreateTime(DateUtils.getNowDate());

        tblSaleReturn.setCreateById(loginUser.getUser().getUserId().toString());
        tblSaleReturn.setCreateBy(loginUser.getUser().getUserName());

        int rows = tblSaleReturnMapper.insertTblSaleReturn(tblSaleReturn);
        insertTblSaleReturnDetail(tblSaleReturn);
        return rows;
    }

    /**
     * 修改销售退单
     *
     * @param tblSaleReturn 销售退单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblSaleReturn(TblSaleReturn tblSaleReturn)
    {
        if(tblSaleReturn.getStatus()==3){
            for (TblSaleReturnDetail tblSaleReturnDetail : tblSaleReturn.getTblSaleReturnDetailList()) {
                //库存数量
                long stockNum = tblSaleReturnDetail.getBaseDataProduct().getTblProductStock().getStockNum()+tblSaleReturnDetail.getReturnNum();
                tblSaleReturnDetail.getBaseDataProduct().getTblProductStock().setStockNum(stockNum);
                //库存数量的含税总金额
                BigDecimal taxAmount = BigDecimal.valueOf(stockNum).multiply(tblSaleReturnDetail.getBaseDataProduct().getTblProductStock().getTaxPrice());
                tblSaleReturnDetail.getBaseDataProduct().getTblProductStock().setTaxAmount(taxAmount);
                //修改商品库存
                tblProductStockMapper.updateTblProductStock(tblSaleReturnDetail.getBaseDataProduct().getTblProductStock());
            }
        }
        tblSaleReturn.setUpdateTime(DateUtils.getNowDate());

        tblSaleReturn.setUpdateById(loginUser.getUser().getUserId().toString());
        tblSaleReturn.setUpdateBy(loginUser.getUser().getUserName());
        if (tblSaleReturn.getApproveBy()!=null){
            tblSaleReturn.setApproveBy((loginUser.getUser().getUserName()));
        }

        tblSaleReturnMapper.deleteTblSaleReturnDetailByReturnId(tblSaleReturn.getId());
        insertTblSaleReturnDetail(tblSaleReturn);
        return tblSaleReturnMapper.updateTblSaleReturn(tblSaleReturn);
    }

    /**
     * 批量删除销售退单
     *
     * @param ids 需要删除的销售退单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblSaleReturnByIds(String[] ids)
    {
        tblSaleReturnMapper.deleteTblSaleReturnDetailByReturnIds(ids);
        return tblSaleReturnMapper.deleteTblSaleReturnByIds(ids);
    }

    /**
     * 删除销售退单信息
     *
     * @param id 销售退单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblSaleReturnById(String id)
    {
        tblSaleReturnMapper.deleteTblSaleReturnDetailByReturnId(id);
        return tblSaleReturnMapper.deleteTblSaleReturnById(id);
    }

    /**
     * 新增销售退单明细信息
     *
     * @param tblSaleReturn 销售退单对象
     */
    public void insertTblSaleReturnDetail(TblSaleReturn tblSaleReturn)
    {
        List<TblSaleReturnDetail> tblSaleReturnDetailList = tblSaleReturn.getTblSaleReturnDetailList();
        String id = tblSaleReturn.getId();
        if (StringUtils.isNotNull(tblSaleReturnDetailList))
        {
            List<TblSaleReturnDetail> list = new ArrayList<TblSaleReturnDetail>();
            for (TblSaleReturnDetail tblSaleReturnDetail : tblSaleReturnDetailList)
            {
                tblSaleReturnDetail.setReturnId(id);
                list.add(tblSaleReturnDetail);
            }
            if (list.size() > 0)
            {
                tblSaleReturnMapper.batchTblSaleReturnDetail(list);
            }
        }
    }
}
